// JavaScript Document
define(function(require) {
	//观察者
	observer = function(){
		return new observer;//jquery式 无new实力化	
	}
	observer.readyPublish = [];
	//订阅
	observer.addSubscriber = function(callback) {
		this.suberscribers = [],
		this.suberscribers[this.suberscribers.length] = callback;
	}
	//退订
	observer.removeSuberscriber = function(callback) {
		for(var i = 0, suber; suber = this.subscribers[i++];) {
			if(suber == callback) {
				delete suber;	
			}	
		}	
	}
	//发布
	observer.publish = function() {
		for(var i = 0, puber; puber = this.readyPublish[i++];) {
			this.suberscribers[0](mes);
			puber.tag.suberscribers[0](data);	
		}
	}
	//将对象o具有观测者功能
	observer.make = function(o) {
		for(var i in this) {
			o[i] = this[i];
			o.subscribers = [];	
		}
	}
	//当前修改的元素集合
	observer.changedEles = [];
	/*获取发布者并绑定事件
	*@param {object} tags 当前绑定的对象
	*@param {array} data 初始值
	*@param {string} 绑定对象属于哪个模块, basic flw right 3个值
	*/
	observer.setPuber = function(tags, data, type) {
		for(var i = 0, tag; tag = tags[i++];) {
			tag.onchange = function() {
				observer.monitoring(this, data, type);
			}
		}
	}
	observer.monitoring = function(that, data, type) {
		var num, arr, checkVal;
		num = inArray(this.changedEles, that);
		if(false === num) {
			this.changedEles.push(that);
			this.addUpdate(that, data, type);	
		}
		else {
			if("checkbox" == that.getAttribute("type")) {
				if(that.checked) {
					checkVal = 1;
				}	
				else {
					checkVal = 0;	
				}
			}
			else {
				checkVal = that.value.trim();	
			}	
			dataId = that.getAttribute("data-form");      //chang后的值 和初始值进行比较。
			if(checkVal == data[dataId].trim()) {    //相同，说明用户将值改回去了，从更新语句中删除该更新
				this.changedEles.splice(num, 1);
				arr = _getTagFromPublish(that);
				this.readyPublish[arr[0]].tags.splice(arr[1], 1);
				this.readyPublish[arr[0]].update.splice(arr[1], 1);
				if(0 == this.readyPublish[arr[0]].tags.length) {
					this.readyPublish.splice(arr[0], 1);	
				}
			}
			else {         //不同，将更新语句中该元素的值更新。
				arr = _getTagFromPublish(that);
				name = that.getAttribute("name");
				this.readyPublish[arr[0]].update[arr[1]] = name + "='"+checkVal+"'";
			}
		}

		function _getTagFromPublish(tag) {
			var publish = observer.readyPublish,
				num, curPublish;
			for(var i = 0, len = publish.length; i < len; i++) {
				curPublish = publish[i]
				num = inArray(curPublish.tags, tag);
				if(false === num) {
					continue;	
				}	
				else {
					return [i, num];	
				}
			}
		}
	}
	observer.addUpdate =  function(tag, data, type) {
		var puber = {
				tags : [],
				header : '',
				where : '',
				update : []	
			},
			name, val, tempWhere;
		puber.tags.push(tag);
		
		name = tag.getAttribute("name");
		if("checkbox" == tag.getAttribute("type")) {
			if(tag.checked) {
				val = 1;
			}	
			else {
				val = 0;	
			}
		}
		else {
			val = tag.value.trim();	
		}	
		//先构造出where条件， 如果有相同的where的条件，只需要把 set值添加到相同的where条件对象的update里面即可。
		switch(type) {
			case "basic":
				puber.header = "bas";
				tempWhere = "ASNM="+data[20]+"&STPID="+data[0]+"&FLWID="+data[19]+"&COHRID="+data[7]+"&DLFLG='0'";
				break;
			case "flw":
				puber.header = "flw";
				tempWhere ="ASNM="+data[12]+"&COHRID="+data[5]+"&FLWID="+data[10]+"&STPID="+data[11]+"&STPCHID='"+data[6]+"&DLFLG=0&RLTTP="+data[3]+"&RLTGP="+data[4];
				break;
			case "right":
				puber.header = "right";
				tempWhere = "ASNM="+data[8]+"&"+data[6]+"&OHRID="+data[2]+"&PRVTYPE="+data[3]+"&SQNM="+data[7];
				break;
			default:
				break;	
		}	
		
		for(var i = 0, curPuber, len = this.readyPublish.length; i < len; i++) {
			curPuber = this.readyPublish[i];
			if(curPuber.where == tempWhere) {
				curPuber.tags.push(tag);
				curPuber.update.push(name+"="+val);      //这里表示已经添加完毕
				return;	
			}	
		}
		puber.update.push(name+"="+val);                 //执行到这里，该条修改记录第一次出现。
		puber.where = tempWhere;
		puber.data = data;
		this.readyPublish.push(puber)
	}
	
	function inArray(array, tag) {
		for(var i = 0, len = array.length; i < len; i++) {
			if(tag == array[i]) {
				return i;	
			}	
		}	
		return false;
	}
	return observer;
})